// OGoMojo
// Whitemice.MOGI.MOGI (Utility class)
// License: LGPL
// Copyright: Whitemice Consulting, 2006
// Author(s): Adam Tauno Williams (awilliam@whitemice.org)
using System;

namespace Whitemice.MOGI {
	/// \brief This is a static object the provides serveral simple helper methods
	public static class MOGI {
	
			/// \brief Returns the last portion of the documentId string
			/// \param documentId A MOGI document id
			/// \warn This method is not value for Relation objects
			public static string Id(string documentId)
			{
				return (documentId.Substring(documentId.LastIndexOf('/') + 1));
			}
			
			/// \brief Return Id tag of an IMOGIObject
			/// \param document An IMOGIObject
			/// \note This method passes the name of the object to Id(string documentId)
			/// \todo Throw an exception if the object is a resource
			public static string Id(IMOGIObject document)
			{
				return Id(document.Name);
			}
			
			/// \brief Produces a documentId string for a given documentType and id
			/// \param documentType The type of document bieng named
			/// \param id The document id
			public static string DocumentId(DocumentType documentType, string id) 
			{
				string documentId = null;

				switch (documentType) 
				{
					case DocumentType.Contact: documentId = "Document/Contact/" + id.ToString(); break;
					case DocumentType.Enterprise: documentId = "Document/Enterprise/" + id.ToString();	break;
					case DocumentType.Project:documentId = "Document/Project/" + id.ToString(); break;
					case DocumentType.Task:	documentId = "Document/Task/" + id.ToString(); break;
					case DocumentType.Appointment: documentId = "Document/Appointment/" + id.ToString(); break;
					case DocumentType.Account: documentId = "Document/Account/" + id.ToString(); break;
					case DocumentType.Resource: documentId = "Document/Resource/" + id.ToString();	break;
					case DocumentType.Team: documentId = "Document/Team/" + id.ToString();	break;
					case DocumentType.AuditEntry: documentId = "Document/AuditEntry/" + id.ToString();	break;
					case DocumentType.BLOB: documentId = "Document/BLOB/" + id.ToString();	break;
				}
				return documentId;
			}
			
			/// \brief Test if id passed is an OGo Built-In Id
			/// \param id  Primary Key id to test
			public static bool IsBuiltIn(string id)
			{
				if((Convert.ToInt32(id) % 2) == 0)
					return false;
				return true;
			}
						
			/// \brief Returns a document type from a document name
			/// \param id A document name string such as Document/Contact/10120
			/// \note For information on the formats of names see the IMOGIObject interface,
			/// this routine will take either a name or a simple type string like "Contact".
			public static DocumentType DetermineType(string id) 
			{
					DocumentType documentType;
					string tag = null;
					
					documentType = DocumentType.Undefined;
					switch (id.Split('/').Length)
					{
						case 3: tag = id.Split('/')[1]; break;
						case 4: tag = id.Split('/')[2]; break;
						default: tag = id; break;
					}
					switch (tag) 
					{
						case "Contact": documentType = DocumentType.Contact; break;
						case "Enterprise": documentType = DocumentType.Enterprise;	break;
						case "Project": documentType = DocumentType.Project; break;
						case "Task": documentType = DocumentType.Task;	break;
						case "Appointment": documentType = DocumentType.Appointment; break;
						case "Account": documentType = DocumentType.Account; break;
						case "Resource": documentType = DocumentType.Resource; break;
						case "Team": documentType = DocumentType.Team;	break;	
						case "AuditEntry": documentType = DocumentType.AuditEntry;	break;
						case "BLOB": documentType = DocumentType.BLOB;	break;
					}
					return documentType;
			}
			
			/// \brief Get the document type as a string, like "Contact"
			/// \note This uses the DetermineType(string) method
			public static string DocumentTypeAsString(string id)
			{
				switch(MOGI.DetermineType(id))
				{
					case DocumentType.Contact: return "Contact";
					case DocumentType.Enterprise: return "Enterprise";
					case DocumentType.Project: return "Project";
					case DocumentType.Task: return "Task";
					case DocumentType.Appointment: return "Appointment";
					case DocumentType.Account: return "Account";
					case DocumentType.Resource: return "Resource";
					case DocumentType.Team: return "Team";
					case DocumentType.AuditEntry: return "AuditEntry";
					case DocumentType.BLOB: return "BLOB";
					default: return "unknown";
				}
			}
			
			/// \brief Get the description of a task status value
			/// \param status A task status value
			/// \todo Move these values into the configuration
			public static string TaskStatusDescription(ETaskStatus status)
			{
				switch(status)
				{
					case ETaskStatus.Accepted: return "Accepted";
					case ETaskStatus.Archived: return "Archived";
					case ETaskStatus.Commented: return "Commented";
					case ETaskStatus.Created: return "Created";
					case ETaskStatus.Divided: return "Divided";
					case ETaskStatus.Done: return "Done";
					case ETaskStatus.Processing: return "Processing";
					case ETaskStatus.Reactivated: return "Reactivated";
					case ETaskStatus.Rejected: return "Rejected";
				}
				return "Unknown";
			}
			
			/// \brief Get the description of a task priority value
			/// \param priority
			/// \todo Move these values into the configuration
			public static string TaskPriorityDescription(ETaskPriority priority)
			{
				switch(priority)
				{
					case ETaskPriority.Average: return "Average";
					case ETaskPriority.High: return "High";
					case ETaskPriority.Low: return "Low";
					case ETaskPriority.VeryHigh: return "Very High";
					case ETaskPriority.VeryLow: return "Very Low";
				}
				return "Undefined";
			}
			
			/// \brief Get the description of a task sensitivity value
			/// \param sensitivity
			/// \todo Move these values into the configuration
			public static string TaskSensitivityDescription(ETaskSensitivity sensitivity)
			{
				switch(sensitivity)
				{
					case ETaskSensitivity.Public: return "Public";
					case ETaskSensitivity.Personal: return "Personal";
					case ETaskSensitivity.Private: return "Private";
					case ETaskSensitivity.Confidential: return "Confidential";				
				}
				return "Undefined";
			}
			
			public static string RelationTypeAsString(ERelationType relation)
			{
				switch(relation)
				{
					case ERelationType.AssignedTo: return "AssignedTo";
					case ERelationType.IsAlso: return "IsAlso";
					case ERelationType.Member: return "Member";
					case ERelationType.MemberOf: return "MemberOf";
					case ERelationType.ObjectLink: return "ObjectLink";
					case ERelationType.Owner: return "Owner";
				}
				return "Unknown";
			}
			
			public static ERelationType RelationType(string relation)
			{
				switch(relation)
				{
					case "AssignedTo": return ERelationType.AssignedTo;
					case "IsAlso": return ERelationType.IsAlso;
					case "Member": return ERelationType.Member;
					case "MemberOf": return ERelationType.MemberOf;
					case "ObjectLink": return ERelationType.ObjectLink;
					case "Owner": return ERelationType.Owner;
				}
				return ERelationType.Unknown;
			}
	}
}